package com.example.communist_app_api.controller.uniapp;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/27 9:50
 */

import com.example.communist_app_api.db.dto.ActivityDTO;
import com.example.communist_app_api.db.dto.ActivityUniAppShowDTO;
import com.example.communist_app_api.service.ActivityService;
import com.example.communist_app_api.service.UserActivityService;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.UniAppActivityReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppActivitySignReq;
import com.example.communist_app_api.vo.uniapp.req.UniAppShowActivityByConditionReq;
import com.example.communist_app_api.vo.uniapp.resp.UniAppMyActivityDetailResp;
import com.example.communist_app_api.vo.uniapp.resp.UniAppMyActivityRecordResp;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

//小程序组织活动
@RestController
@RequestMapping("/uniApp/activity")
public class UniAppActivityController {
    @Autowired
    private ActivityService activityService;
    @Autowired
    private UserActivityService userActivityService;

    @PostMapping("/show")
    @ApiOperation("小程序显示可参加活动")
    public R showActivityByCondition(@RequestBody UniAppShowActivityByConditionReq req, HttpServletRequest httpServletRequest){
        //查询全部活动，分为可报名和结束报名两种状态，可以进行搜索,显示以开始时间倒序显示
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        List<ActivityUniAppShowDTO>  activityList = activityService.uniAppShowActivityByCondition(req,userId);
        return R.ok("查询成功").put("activityList",activityList);
    }

    @PostMapping("/activityDetail")
    @ApiOperation("小程序活动详情")
    public R activityDetail(@Valid  @RequestBody UniAppActivityReq req, HttpServletRequest httpServletRequest){
        //查询全部活动，分为可报名和结束报名两种状态，可以进行搜索,显示以开始时间倒序显示
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        ActivityDTO activityDTO = activityService.selectUniAppActivityById(req.getActivityId());
        //页面需要title,creatTime,applicationNum,activityRequirement,createdTime,context
        //ApplicationNumber已报名人数
        Integer applicationNumber = userActivityService.getApplicationNumberByActivityId(req.getActivityId());
        //查询使用小程序的用户本人是否参与报名，flag 为 true时已报名 , 为false时为未报名
        Boolean flag = userActivityService.getExistenceByUserIdAndActivityId(userId,req.getActivityId());
        //报名状态 报名人数已满 , 可报名 , 已报名
        String message = "报名人数已满";
        activityDTO.setApplicationNum(applicationNumber);
        if (activityDTO.getLimitedNum() > applicationNumber && !flag) {
            message = "可报名";
        }else if (activityDTO.getLimitedNum() == applicationNumber && !flag) {
            message = "报名人数已满";
        }else if (flag) {
            message = "已报名";
        }
        return R.ok("查询成功").put("activityDTO",activityDTO).put("message",message);
    }

    @PostMapping("/signUpActivity")
    @ApiOperation("我要报名")
    public R signUpActivity(@Valid  @RequestBody UniAppActivityReq req,HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        userActivityService.insertByUserIdAndActivityId(userId,req.getActivityId());
        return R.ok("报名成功");
    }

    @GetMapping("/myActivity")
    @ApiOperation("我的服务")
    public R myActivity(HttpServletRequest httpServletRequest,@RequestParam(value = "title",required = false) String title){
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        //获取用户个人参与的活动
        UniAppMyActivityRecordResp resp = userActivityService.getAllActivityByUserId(title,userId);
        return R.ok().put("resp",resp);
    }

    @PostMapping("/myActivityDetail")
    @ApiOperation("我的服务详情")
    public R myActivityDetail(@Valid  @RequestBody UniAppActivityReq req,HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        UniAppMyActivityDetailResp resp = activityService.getMyActivityDetailByActivityIdAndUserId(req.getActivityId(),userId);
        return R.ok("查询成功").put("resp",resp);
    }


    @GetMapping("/generateSignCode")
    @ApiOperation("生成签到验证码")
    public R generateSignLink(@Valid  @RequestBody UniAppActivityReq req){
        String signCode=activityService.generateSignCode(req.getActivityId());
        return R.ok().put("signCode",signCode);
    }


    @GetMapping("/sign")
    @ApiOperation("签到")
    public R generateSignLink(@Valid @RequestBody UniAppActivitySignReq req, HttpServletRequest request){
        Integer userId = getUserIdByUniAppToken(request);
        activityService.sign(req.getActivityId(),req.getCode(), userId);
        return R.ok("签到成功");
    }

    @PostMapping("/cancel")
    @ApiOperation("取消报名")
    public R cancelActivity(@Valid  @RequestBody UniAppActivityReq req,HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        userActivityService.cancelActivitySignUp(req.getActivityId(),userId);
        return R.ok("取消报名成功！");
    }

    private Integer getUserIdByUniAppToken(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String id_String = claims.getSubject();
        //获取当前登录小程序的用户Id
        int userId = Integer.parseInt(id_String);
        return userId;
    }








}
